﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESPlus.Application.Basic.Passive;
using ESPlus.Rapid;
using ESPlus.Application.Basic;
using Chat;
using OMCS.Passive;

namespace Chat
{
    public partial class LoginForm : Form
    {
        private RapidPassiveEngine rapidPassiveEngine;
        private IMultimediaManager multimediaManager;
        //private RapidPassiveE rapidPassiveEngine;
        private MainForm mainForm;
        private string userID;
        public string UserID
        {
            get { return userID; }
        }

        public LoginForm(RapidPassiveEngine engine,IMultimediaManager mgr, MainForm _mainForm)
        {
            InitializeComponent();
            this.rapidPassiveEngine = engine;
            this.multimediaManager = mgr;
            this.mainForm = _mainForm;
        }

        private void button_login_Click(object sender, EventArgs e)
        {
            this.userID = this.textBox_id.Text.Trim();
            if (userID.Length > 10)
            {
                MessageBox.Show("ID长度必须小于10.");
                return;
            }

            LogonResult logonResult = LogonResult.Succeed;
            try
            {
                this.rapidPassiveEngine.SystemToken = "";  //系统标志
                //初始化引擎并登陆，返回登录结果。如果登陆成功，引擎将与当前用户绑定。
              
                logonResult = this.rapidPassiveEngine.Initialize(userID, this.textBox_pwd.Text, "192.168.1.30", 4530, this.mainForm).LogonResult;
                
            }
            catch (Exception ee)
            {
                MessageBox.Show(string.Format("连接服务器失败。{0}", ee.Message));
                return;
            }

            if (logonResult == LogonResult.Failed)
            {
                MessageBox.Show("用户名或密码错误！");
                return;
            }

            if (logonResult == LogonResult.HadLoggedOn)
            {
                MessageBox.Show("已经在其他地方登陆！");
            }

            try
            {
                this.multimediaManager.Initialize(this.textBox_id.Text, this.textBox_pwd.Text, "127.0.0.1", 9900);
            }
            catch (Exception ee)
            {
                MessageBox.Show("初始化多媒体设备管理器失败！" + ee.Message);
            }

            this.DialogResult = DialogResult.OK;
        }


    }
}
